﻿using Infrastructure.Common;
using Infrastructure.DataAccess;
using Infrastructure.IRepository;
using Infrastructure.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace ApiServer.ServiceExtensions
{
    public static class ServiceExtension
    {
        /// <summary>
        /// 配置数据库上下文
        /// </summary>
        /// <param name="services"></param>
        public static void AddSetDbContext(this IServiceCollection services,IConfiguration configuration)
        {
            //获取到当前的要访问的数据库
            string dbStrType = configuration["SqlType"];


            if (dbStrType=="MySqlStr")
            {
                var connstr = configuration.GetConnectionString("MySqlStr");
                //注册MySQL上下文
                services.AddDbContext<DbContext, MySqlDbContext>(m=>m.UseMySql(connstr, ServerVersion.AutoDetect(connstr)));
                //Console.WriteLine($"连接数据库的类型是：**************************MySqlStr");
            }
            else if (dbStrType == "SqlServerStr")
            {
                var s = configuration.GetConnectionString("SqlServerStr");
                //注入sqlserver上下文
                //Console.WriteLine($"连接数据库的类型是：**************************SqlServerStr");
                services.AddDbContext<DbContext, SqlServerDbContext>(m=>m.UseSqlServer(s));
            }
            Console.WriteLine($"连接数据库的类型是：**************************{dbStrType}");
            //services.AddScoped<IUnitOfWork, UnitOfWork>();

        }
    }
}
